<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闭包-单例</title>
</head>

<body>
    <script>
        // 回到函数，闭包
        // 自由变量 唯一的实例
        function StorageBase() {

        }
        const Storage = (function () {
            let instance;
            return function () {
                if (!instance) {
                    instance = new StorageBase();
                }
                return instance;
            }
        })()
        StorageBase.prototype.setItem = function (key, value) {
            localStorage.setItem(key, value);
        }
        StorageBase.prototype.getItem = function (key) {
            return localStorage.getItem(key);
        }
        const storage1 = new Storage();
        const storage2 = new Storage();
        // console.log(storage1 === storage2);
        storage1.setItem('name', '张三');
        console.log(storage1.getItem('name'));
    </script>
</body>

</html>